package com.example.rightmost.service.Impl;

import com.example.rightmost.dao.FansMapper;
import com.example.rightmost.dao.PieceMapper;
import com.example.rightmost.dao.UserMapper;
import com.example.rightmost.entity.*;
import com.example.rightmost.service.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author:javayang
 * @date:2024/8/24
 * @description:
 */
@Service
public class HomePageServiceImpl implements HomePageService {

    @Autowired
    FansMapper fansMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PieceMapper pieceMapper;

    //粉丝
    @Override
    public Integer getFans(Integer id) {
        return fansMapper.getFansCount(id);
    }

    //关注
    @Override
    public Integer getFollow(Integer id) {
        return fansMapper.getFollowCount(id);
    }

    //修改
    @Override
    public Result updateInfo(User user) {
        user.setLastUpdateTime(LocalDateTime.now());
        int i = userMapper.updateInfo(user);
        if (i > 0) {
            return Result.success();
        } else {
            return Result.error("修改错误");
        }
    }

    //我的作品
    @Override
    public List<Piece> getMyPiecesList(Piece piece) {
        return pieceMapper.getMyPiecesList(piece);
    }

    //关注
    @Override
    public Result setFollow(Fans fans) {
        int i = fansMapper.setFollow(fans);
        if (i > 0){
            return Result.success();
        } else {
            return Result.error("关注失败");
        }
    }

    //取消关注
    @Override
    public Result delFollow(Fans fans) {
        int i = fansMapper.delFollow(fans);
        if (i > 0){
            return Result.success();
        } else {
            return Result.error("取关失败");
        }
    }

    //判断关注状态
    @Override
    public Result getFollowInfo(Fans fans) {
        if(fans.getUserId().equals(fans.getFollowId())){
            return Result.success();
        } else {
            Integer i = fansMapper.getFollowInfo(fans);
            if (i == null){
                return Result.success(false);
            } else {
                return Result.success(true);
            }
        }

    }

}
